const db = require('../db/index')

// 获取宠物美容信息列表
exports.petCareList = (req, res) => {
  const sql = `select * from  pet_care`
  db.query(sql, (err, results) => {
    if (err) return res.cc(err)
    if (results.length < 0) return res.cc('获取宠物美容信息列表失败！')
    res.send({
      status: 0,
      message: '获取宠物美容信息列表成功！',
      data: results
    })
  })
}

// 后台-宠物美容管理列表
exports.managePetCare = (req, res) => {
  // 每页几条数据
  let pagesize = 5
  // 当前页码数
  let pagenum = (req.query.pagenum - 1) * pagesize
  let datas = []
  const sql = `select * from pet_care limit ${pagenum},${pagesize}`
  const sql2 = `select count(*) as total from pet_care `
  db.query(sql, (err, results) => {
    if (err) return res.cc(err)
    if (results.length < 0) return res.cc('获取用户信息列表失败！')
    ;(datas = results),
      db.query(sql2, (err, result) => {
        if (err) return res.cc(err)
        if (result.length < 0) return res.cc('获取用户信息列表失败！')
        res.send({
          status: 0,
          message: '获取用户信息列表成功！',
          data: datas,
          total: result[0]
        })
      })
  })
}

// 发布宠物美容信息
exports.insertPetCareList = (req, res) => {
  const sql = `insert into pet_care set cname = ?,price = ?,member_price = ?`
  db.query(sql, [req.body.cname, req.body.price, req.body.member_price], (err, results) => {
    if (err) return res.cc(err)
    if (results.affectedRows !== 1) return res.cc('添加失败！')
    res.send({
      status: 0,
      message: '添加成功！'
    })
  })
}

// 根据cid删除信息
exports.deleteByCid = (req, res) => {
  const sql = `delete from pet_care where cid = ? `
  db.query(sql, req.body.cid, (err, results) => {
    console.log(req.body)
    if (err) return res.cc(err)
    if (results.affectedRows !== 1) return res.cc('删除失败！')
    res.cc('删除成功！', 0)
  })
}

// 修改宠物美容信息
exports.updatePetCare = (req, res) => {
  const sql = `update pet_care set cname = ?,price = ? ,member_price = ? where cid = ? `
  db.query(sql, [req.body.cname, req.body.price, req.body.member_price, req.body.cid], (err, results) => {
    console.log(req.body)
    if (err) return res.cc(err)
    if (results.affectedRows !== 1) return res.cc('更新失败！')
    res.cc('更新成功！', 0)
  })
}
